﻿FlashShow = function(container, interval, activeHand, images)
{
	var me = this;
	this.container = $(container);
	this.interval = interval;
	this.activeHand = activeHand;
	this.index = 0;
	
	var hand_click = function(){ trans.call(me, this.index, true) }
	
	if(images)
	{
		this.container.innerHTML = '';
		images.forEach(function(image, index)
		{
			var img = image.cloneNode(0);
			img.hand = $T(image.father(), { 'index':index, onclick:hand_click });
			this.container.appendChild(img);
		}, this);
		this.images = this.container.child('img');
	}
	else
	{
		var ol = $C('OL');
		this.images = this.container.child('img');
		for(var i=0; i<this.images.length; i++)
			ol.appendChild(this.images[i].hand = $C('LI', { index:i, onclick:hand_click }));
		this.container.appendChild(ol);
	}

	this.images.forEach(function(image) { image.opacity(0) });
	
	function trans(index, manual)
	{
		if(index != this.index && !this.running)
		{
			if(this.timer) clearInterval(this.timer);
			this.run(this.images[this.index], this.images[this.index = index]);
			this.start(manual);
		}
	};
	
	this.trans = function()
	{
		me.run.call(me, me.images[me.index++], me.images[me.index] || me.images[me.index = 0]);
	}

	if(this.onStart) this.onStart.call(this, this.images);
	this.running = false;
};
FlashShow.prototype.start = function(manual)
{
	if(!manual) this.run.call(this, null, this.images[0]);
	if(this.images.length >= 2)
		this.timer = setInterval(this.trans, this.interval);
};
FlashShow.prototype.run = function(a, b)//将被隐藏的图片, 将被显示的图片
{
	var me = this;
	me.running = true;
	if(a) a.hand.className = '';
	b.hand.className = this.activeHand;
	b.show();
	if(this.beforeTrans) this.beforeTrans.call(this, a, b, b.hand);
	[0,1,2,3,4,5,6,7,8,9,10].setInterval(function(alpha)
	{
		if(a) a.opacity(100 - alpha * 10);
		b.opacity(alpha * 10);
		if(alpha == 10)
		{
			if(a) a.hide();
			me.running = false;
			if(this.afterTrans) this.afterTrans.call(this, a, b);
		}
	}, 50);
}
